//
//  MUploadDataViewController.m
//  Gunz
//
//  Created by Joggy on 16/8/17.
//  Copyright © 2016年 NUPT. All rights reserved.
//

#import "MUploadDataViewController.h"
#import "Step3InputView.h"
#import "Step3InputField.h"
#import "MDataViewController.h"
#import "BarButtonView.h"
#import "QYNotice.h"

@interface MUploadDataViewController () {
    
    UIScrollView *scroll;
    NSString *dataPath;
    NSString *dataDescribe;
    Step3InputField *dataPathField;
    Step3InputView *dataDescribeView;
    Step3InputView *dataCheckView;
    
}

@end

@implementation MUploadDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self prepareForNav];
    [self prepareForLayout];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endEdit)];
    [self.view addGestureRecognizer:tap];
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveDataPath:) name:GZPathOfSelectedDataNotification object:nil];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)prepareForNav {
    [super prepareForNav];
    self.title = @"上传任务";
    //导航左按键
    UIImage *image = [UIImage imageNamed:@"all_back"];
    UIButton *leftBarButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBarButton.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    leftBarButton.layer.position = CGPointMake(22, 22);
    [leftBarButton setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [leftBarButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBarButton];
}

- (void)prepareForLayout {
    [super prepareForLayout];
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight - 64)];
    scroll.backgroundColor = [UIColor clearColor];
    [self.view addSubview:scroll];
    
    Step3InputField *joinTime = [[Step3InputField alloc] initWithTitle:@"开始时间:"];
    joinTime.center = CGPointMake(screenWidth/2, 36);
    joinTime.textField.text = _data[@"jointime"];
    joinTime.textField.enabled = false;
    joinTime.textField.backgroundColor = [UIColor clearColor];
    joinTime.textField.layer.borderColor = [UIColor clearColor].CGColor;
    [scroll addSubview:joinTime];
    Step3InputField *taskId = [[Step3InputField alloc] initWithTitle:@"任务单号:"];
    taskId.center = CGPointMake(screenWidth/2, joinTime.center.y + 36);
    taskId.textField.text = _data[@"taskid"];
    taskId.textField.enabled = false;
    taskId.textField.backgroundColor = [UIColor clearColor];
    taskId.textField.layer.borderColor = [UIColor clearColor].CGColor;
    [scroll addSubview:taskId];
    
    
    dataPathField = [[Step3InputField alloc] initWithTitle:@"上传数据:"];
    dataPathField.center = CGPointMake(screenWidth/2, taskId.center.y + 36);
    dataPathField.textField.enabled = false;
    [scroll addSubview:dataPathField];
    dataDescribeView = [[Step3InputView alloc] initWithTitle:@"数据说明:"];
    dataDescribeView.center = CGPointMake(screenWidth/2, dataPathField.center.y + 61);
    [scroll addSubview:dataDescribeView];
    dataCheckView = [[Step3InputView alloc] initWithTitle:@"审核意见:"];
    [dataCheckView.textView setEditable:false];
    dataCheckView.textView.text = @"暂无";
    dataCheckView.center = CGPointMake(screenWidth/2, dataDescribeView.center.y + 61 + 12);
    [scroll addSubview:dataCheckView];
    
    CGFloat bottom = dataCheckView.frame.origin.y + dataCheckView.frame.size.height;
    CGFloat contentSizeY = bottom > screenHeight ? bottom : screenHeight - 63;
    scroll.contentSize = CGSizeMake(screenWidth, contentSizeY);
    
    UIButton *uploadButton = [UIButton buttonWithType:UIButtonTypeCustom];
    uploadButton.frame = CGRectMake(0, 0, 30, 30);
    uploadButton.center = CGPointMake(screenWidth - 36, dataPathField.center.y);
    [uploadButton setImage:[UIImage imageNamed:@"mine_upload"] forState:UIControlStateNormal];
    [uploadButton addTarget:self action:@selector(selectUploadData) forControlEvents:UIControlEventTouchUpInside];
    [scroll addSubview:uploadButton];
    BarButtonView *barButton = [[BarButtonView alloc] initWithTitle:@"上传"];
    [barButton.but addTarget:self action:@selector(uploadData) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:barButton];
}

- (void)uploadData {
    NSLog(@"%@", @"upload");
    [QYNotice showSuccess:@"上传成功" toView:self.view];
    [GZConst delay:3 with:^{
        [self back];
    }];
    
}

- (void)receiveDataPath:(NSNotification*)notification {
    NSLog(@"%@", notification.userInfo);
    dataPath = notification.userInfo[@"dataPath"];
    NSString *dataName = [[dataPath componentsSeparatedByString:@"/"] lastObject];
    dataPathField.textField.text = dataName;
}

- (void)selectUploadData {
    MDataViewController *dataView = [[MDataViewController alloc] init];
    dataView.isPresent = true;
    UINavigationController *dataNav = [[UINavigationController alloc] initWithRootViewController:dataView];
    [self presentViewController:dataNav animated:true completion:nil];
}

- (void)back {
    [self.navigationController popViewControllerAnimated:true];
}

- (void)endEdit {
    [self.view endEditing:true];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
